﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormDoiTacThem : DevComponents.DotNetBar.RibbonForm
    {
        public FormDoiTacThem()
        {
            InitializeComponent();
        }
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private void FormDoiTacThem_Load(object sender, EventArgs e)
        {
            GetListNhomDoiTac();
        }
        private void bttLuu_Click(object sender, EventArgs e)
        {
            Create();
            Reset();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void cbbNhomDoiTac_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtTenDoiTac_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtDiaChi_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtNguoiLienHe_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtDienThoai_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtFax_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtMaSoThue_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }
        void GetListNhomDoiTac()
        {
            cbbNhomDoiTac.DataSource = db.NhomDoiTacs.ToList();
            cbbNhomDoiTac.DisplayMember = "TenNhomDoiTac";
            cbbNhomDoiTac.ValueMember = "Id";
            cbbNhomDoiTac.DropDownColumns = "Id,TenNhomDoiTac";
            cbbNhomDoiTac.DropDownColumnsHeaders = "Mã nhóm\r\nTên nhóm đối tác\r\n";
        }

        void Reset()
        {
            txtDiaChi.Text = "";
            txtDienGiai.Text = "";
            txtDienThoai.Text = "";
            txtFax.Text = "";
            txtMaSoThue.Text = "";
            txtNguoiLienHe.Text = "";
            txtTenDoiTac.Text = "";
            cbbNhomDoiTac.SelectedValue = 1;
            bttLuu.Enabled = false;
            GetListNhomDoiTac();
        }

        void KiemTra()
        {
            if (txtDiaChi.Text != "" && txtDienGiai.Text != "" && txtDienThoai.Text != "" && txtFax.Text != "" && txtMaSoThue.Text != "" && txtNguoiLienHe.Text != "" && txtTenDoiTac.Text != "" && Convert.ToInt32(cbbNhomDoiTac.SelectedValue) != 0)
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }

        void Create()
        {
            try
            {
                var t = new DoiTac();
                t.TenDoiTac = txtTenDoiTac.Text;
                t.NhomDoiTacId = Convert.ToInt32(cbbNhomDoiTac.SelectedValue);
                t.NguoiLienHe = txtNguoiLienHe.Text;
                t.MaSoThue = txtMaSoThue.Text;
                t.Fax = txtFax.Text;
                t.DienThoai = txtDienThoai.Text;
                t.DienGiai = txtDienGiai.Text;
                t.DiaChi = txtDiaChi.Text;
                db.DoiTacs.Add(t);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }
    }
}
